package Classes.Managers// ActionScript file
{
	import Classes.GameErrors.GameError;
	import Classes.GameViews.ViewImgs.CartoonViewImg;
	
	import MV_BMP.MovieClip_BMP;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	/**
	 *动画载入管理器 
	 * @author upstream
	 * 
	 */	
	public class M_LoadCartoon
	{
		private static var _instance:M_LoadCartoon;/*静态引用*/
		private var _mainPath:String;/*主路径*/
		private var _cdn:String;/*CDN路径*/
		private var _sourceObj:Object;/*已获取的动画资源名存储器*/
		private var _needLoadAry:Array;/*需求载入数组*/
		private var _nowLoadingObj:Object;/*正在载入的对象*/
		private var _isLoadingTmpSource:Boolean;/*临时资源载入标记*/
		private var _app:ApplicationDomain;/*动画存放域*/
		public function M_LoadCartoon(mainPath:String,cdn:String):void
		{
			_instance=this;
			this._mainPath=mainPath;
			this._cdn=cdn;
			this.init();
		}
		
		private function init():void
		{
			this._sourceObj=new Object;
			this._needLoadAry=new Array;
			this._app=new ApplicationDomain();
		}
		
		/**
		 *载入所有的临时动画资源 
		 * @param sourceNamesAry 临时动画资源名数组
		 * 
		 */		
		public function loadTmpSources(sourceNamesAry:Array):void
		{
			this.isLoadingTmpSource=true;
			for each(var sourceName:String in sourceNamesAry)
			{
				this.loadTmpSource(sourceName);
			}
		}
		
		/**
		 *载入临时动画资源 
		 * @param sourceName 临时动画资源名
		 * 
		 */		
		private function loadTmpSource(sourceName:String):void
		{
			var newNeedLoadObj:Object={sourceName:sourceName,needTargetsAry:[]};
			this.needLoadAry.push(newNeedLoadObj);
			this.loadCheck();
		}
		
		/**
		 *临时资源载入完成 
		 * 
		 */		
		private function tmpSourceLoadOver():void
		{
			this.isLoadingTmpSource=false;
			ZombieTown.instance.tmpSourceLoadOver();
		}
		
		/**
		 *动画资源检测 
		 * @param sourceName 动画资源名
		 * @param needTarget 检测对象
		 * @return 是否具有该资源
		 * 
		 */		
		public function sourceCheck(sourceName:String,needTarget:CartoonViewImg):Boolean
		{
			if(this.sourceObj.hasOwnProperty(sourceName))return true;
			for each(var needLoadObj:Object in this.needLoadAry)
			{
				if(needLoadObj.sourceName==sourceName)
				{
					(needLoadObj.needTargetsAry as Array).push(needTarget);
					return false;
				}
			}
			var newNeedLoadObj:Object={sourceName:sourceName,needTargetsAry:[needTarget]};
			this.needLoadAry.push(newNeedLoadObj);
			this.loadCheck();
			return false;
		}
		
		/**
		 *获取动画 
		 * @param sourceName 动画资源名
		 * @param cartoonName 动画名
		 * @return 动画
		 * 
		 */		
		public function getCartoon(sourceName:String,cartoonName:String):MovieClip_BMP
		{
			if(!this.sourceObj.hasOwnProperty(sourceName))throw new GameError("还没有载入该动画资源");
			for(var k:String in this.sourceObj[sourceName])
			{
				if(k==cartoonName)
				{
					return (this.sourceObj[sourceName][k] as MovieClip_BMP).clone();
				}
			}
			if(!this.app.hasDefinition(cartoonName))throw new GameError("无法获得该动画");
			var newCartoonMV:MovieClip=new (this.app.getDefinition(cartoonName) as Class);
			var newCartoon:MovieClip_BMP=new MovieClip_BMP;
			newCartoon.turnFrom(newCartoonMV);
			this.sourceObj[sourceName][cartoonName]=newCartoon;
			return newCartoon.clone();
		}
		
		/**
		 *载入检测 
		 * 
		 */		
		private function loadCheck():void
		{
			if(!this.nowLoadingObj)
			{
				if(this.needLoadAry.length)
				{
					this.nowLoadingObj=this.needLoadAry[0];
					this.beginLoad();
				}
				else if(this.isLoadingTmpSource)
				{
					this.tmpSourceLoadOver();
				}
			}
		}
		
		/**
		 *开始载入 
		 * 
		 */		
		private function beginLoad():void
		{
			var loader:Loader=new Loader;
			var loaderRequest:URLRequest=new URLRequest(this.mainPath+this.cdn+this.nowLoadingObj.sourceName+".swf");
			var loaderContext:LoaderContext=new LoaderContext(false,this.app)
			loader.load(loaderRequest,loaderContext);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadCompleteHandle);
		}
		
		/**
		 *载入完成 
		 * @param e
		 * 
		 */		
		private function loadCompleteHandle(e:Event):void
		{
			(e.target as LoaderInfo).removeEventListener(Event.COMPLETE,loadCompleteHandle);
			this.sourceObj[this.nowLoadingObj.sourceName]=new Object;
			for each(var needTarget:CartoonViewImg in this.nowLoadingObj.needTargetsAry)
			{
				needTarget.sourceLoadOver();
			}
			this.nowLoadingObj=null;
			this.needLoadAry.shift();
			this.loadCheck();
		}

		/*getter & setter*/
		public static function get instance():M_LoadCartoon
		{
			return _instance;
		}

		public function get mainPath():String
		{
			return _mainPath;
		}

		public function get cdn():String
		{
			return _cdn;
		}
		
		public function get needLoadAry():Array
		{
			return _needLoadAry;
		}

		public function get nowLoadingObj():Object
		{
			return _nowLoadingObj;
		}

		public function set nowLoadingObj(value:Object):void
		{
			_nowLoadingObj = value;
		}

		public function get sourceObj():Object
		{
			return _sourceObj;
		}

		public function get isLoadingTmpSource():Boolean
		{
			return _isLoadingTmpSource;
		}

		public function set isLoadingTmpSource(value:Boolean):void
		{
			_isLoadingTmpSource = value;
		}

		public function get app():ApplicationDomain
		{
			return _app;
		}


	}
}